Imports DAL
Imports System.Configuration
Imports System.Data.SqlClient
Imports MyClasses

Public Class frmProducts

    Public strSearchByCode As String
    Public StrSearchQuery As String          'Stores the values for which search form is called
    Public intSelectedItem As Integer         'Returns Selected Item ID
    Public intProductID As Integer
    Private intRow As Integer
    Public blnCheckStock As Boolean = False
    Public blnShowStock As Boolean = False
    Private btMsg As Byte

    Public keyChar As Char
    Private ActiveGrid As Char 'P: Products      S:Substitute 
    Private iMaxRowDispalyed As Int16 = 12

#Region "FORM EVENTS"
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        'intColumnIndex = 0  'REMOVE HARD CODED VALUE AFTER FINISH
        intSelectedItem = 0
        ' Add any initialization after the InitializeComponent() call.
        fgProducts.MultiSelect = False
    End Sub

    Public Sub New(ByVal intTextBoxWidth As Integer, ByVal intColumnIndexToShow As Integer, Optional ByVal intSelectedItem As Integer = 0)
        ' This call is required by the Windows Form Designer.

        InitializeComponent()
        'intColumnIndex = intColumnIndexToShow
        intSelectedItem = intSelectedItem
        ' Add any initialization after the InitializeComponent() call.
        fgProducts.MultiSelect = False
        fgProducts.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub

    Private Sub frmProducts_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'Dim stTime As DateTime = Now

            Call BCommon.SetFormStandards(Me)
            Call showSercharBy()
            SetGrid()
            fgProducts.Visible = True
            fgProducts.BringToFront()
            Call ReadData()
            intRow = 0
            txtProduct.Text = ""
            txtProduct.Focus()
            fgProducts.CurrentCell = fgProducts.Item(1, 0) ' By default first row.
            If Trim(keyChar) <> String.Empty Then SendKeys.Send(Trim(keyChar))
            keyChar = String.Empty

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmProducts_Load")
        End Try
    End Sub
    'FORM CONTROL EVENTS
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        intSelectedItem = 0 : Me.Close()
    End Sub

    Private Sub txtProduct_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProduct.KeyUp
        Try
            With fgProducts
                If e.KeyCode = Keys.Enter Then
                    If btMsg = 0 Then
                        Call SelectItem()
                        Exit Sub
                    Else : btMsg = 0
                    End If
                Else
                    .Rows(intRow).Selected = False
                    If e.KeyCode = Keys.Up Then
                        intRow -= 1
                    ElseIf e.KeyCode = Keys.Down Then
                        intRow += 1
                    ElseIf e.KeyCode = Keys.Home Then
                        intRow = 1
                    ElseIf e.KeyCode = Keys.End Then
                        intRow = .Rows.Count - 1
                    ElseIf e.KeyCode = Keys.PageUp Then
                        intRow = .FirstDisplayedScrollingRowIndex - iMaxRowDispalyed
                    ElseIf e.KeyCode = Keys.PageDown Then
                        intRow = .FirstDisplayedScrollingRowIndex + iMaxRowDispalyed
                    ElseIf e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right Then
                        Exit Sub
                    Else
                        intRow = FindValue()
                    End If

                End If
                If intRow < 0 Then
                    intRow = 0
                ElseIf intRow >= .Rows.Count Then
                    intRow = .Rows.Count - 1
                End If
                .FirstDisplayedScrollingRowIndex = intRow
                .CurrentCell = .Item(1, intRow)
                .Rows(intRow).Selected = True
            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtProduct_KeyUp")
        End Try
    End Sub

#End Region ' "FORM EVENTS"

#Region "USER DEFINED FUNCTIONS"

    Private Sub showSercharBy()
        Try
            lblSearchBy.Text = strSearchByCode
            'If blnSearchByCode Then
            '    lblSearchBy.Text = strSearchByCode "CODE:"
            'Else
            '    lblSearchBy.Text = "NAME:"
            'End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "showSercharBy")
        End Try
    End Sub

    Public Sub ReadData()
        Try
            Dim ItemReader As SqlDataReader
            ItemReader = DbHelper.ExecuteDataReader(StrSearchQuery)

            With Me.fgProducts
                If ItemReader.IsClosed Then Exit Sub
                Dim intCount As Integer = 0
                .RowCount = 1 ''
                .ColumnCount = ItemReader.FieldCount
                For intColcount As Integer = 4 To ItemReader.FieldCount - 1
                    .Columns(intColcount).Visible = False
                Next
                While ItemReader.Read()
                    .RowCount += 1
                    For intColcount As Integer = 0 To ItemReader.FieldCount - 1
                        .Item(intColcount, intCount).Value = ItemReader(intColcount)
                    Next
                    '.Item(1, intCount).Value = ItemReader(1)
                    '.Item(2, intCount).Value = ItemReader(2)
                    'If strSearchByCode.ToLower.Contains("party") OrElse strSearchByCode.ToLower.Contains("godown") OrElse strSearchByCode.ToLower.Contains("item") Then
                    '    .Item(3, intCount).Value = ItemReader(3)
                    'End If
                    intCount += 1

                End While

            End With
            ItemReader.Close()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ReadData")
        End Try
    End Sub

    Private Sub SelectItem()
        Try
            If btMsg = 1 Then btMsg = 0 : Exit Sub
            With fgProducts
                If .CurrentRow.Index >= 0 Then
                    intSelectedItem = .Item(0, .CurrentRow.Index).Value
                    If blnCheckStock Then
                        If (.Item(3, .CurrentRow.Index).Value > 0) Then
                            Me.Close()
                        Else
                            MessageBox.Show("Zero stock, Please select other Godown", "NiranjanPaints", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                    Else
                        Me.Close()
                    End If
                    '  If intSearchFor = 2 Then

                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SelectItem")
        End Try
    End Sub
    Public Sub SetGrid()
        Try
            With fgProducts
                .Left = 1
                .Top = Me.grpbTop.Height + Me.grpbTop.Top
                .Height = Me.grpbMain.Height - Me.grpbTop.Height
                .Width = Me.grpbMain.Width - 2
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .ScrollBars = ScrollBars.Both

                .ColumnCount = 4
                
                .Columns(0).Visible = False                'ID
                .Columns(0).Name = "ID"
                .Columns(1).Name = "Code"
                .Columns(2).Name = "Name"
                
                .Columns(0).HeaderText = "ID"
                .Columns(1).HeaderText = "CODE"
                .Columns(2).HeaderText = "NAME"
                If strSearchByCode.ToLower.Contains("godown") AndAlso (Me.Owner.Name = FrmStockAdjust.Name) OrElse blnShowStock Then
                    .Columns(3).HeaderText = "Stock"
                    .Columns(1).Width = .Width * 30 / 100
                    .Columns(2).Width = .Width * 30 / 100
                    .Columns(3).Width = .Width * 40 / 100
                ElseIf strSearchByCode.ToLower.Contains("party") Then

                    .Columns(3).HeaderText = "Address"
                    .Columns(1).Width = .Width * 30 / 100
                    .Columns(2).Width = .Width * 30 / 100
                    .Columns(3).Width = .Width * 40 / 100
                Else
                    .Columns(1).Width = .Width * 40 / 100      'ItemCode
                    .Columns(2).Width = .Width * 60 / 100      'Name
                    .Columns(3).Visible = False

                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGrid")
        End Try
    End Sub

#End Region '"USER DEFINED FUNCTIONS"

#Region "GRID EVENTS"

    


    Private Sub fgProducts_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles fgProducts.DoubleClick
        Try
            Call SelectItem()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgProducts_DoubleClick")
        End Try
    End Sub

    Private Sub fgProducts_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fgProducts.KeyUp
        Try
            With fgProducts
                If e.KeyCode = Keys.Home Then
                    .Rows(1).Selected = True
                    e.SuppressKeyPress = False
                    Exit Sub
                ElseIf e.KeyCode = Keys.End Then
                    '  .a = .Rows.Count - 1
                    e.SuppressKeyPress = False
                    Exit Sub
                ElseIf e.KeyCode = Keys.Enter Then
                    Call SelectItem()
                ElseIf e.KeyCode = Keys.Escape Then
                    intSelectedItem = 0
                    Me.Close()
                ElseIf (e.KeyCode <> Keys.Up AndAlso e.KeyCode <> Keys.Down AndAlso e.KeyCode <> Keys.Right AndAlso e.KeyCode <> Keys.Left AndAlso _
                    e.KeyCode <> Keys.PageUp AndAlso e.KeyCode <> Keys.Down) Then
                    ' .TopRow = .Row
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgProducts_KeyDown")
        End Try
    End Sub


    Private Sub FgSubStitute_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Call SelectItem()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "FgSubStitute_DoubleClick")
        End Try
    End Sub
    
#End Region '"GRID EVENTS"

    Function FindValue() As Integer
        Try
            For Each row As DataGridViewRow In fgProducts.Rows
                row.Selected = False
                If Not IsNothing(row.Cells.Item("Code").Value) Then
                    If row.Cells.Item("Code").Value.ToString.ToUpper = txtProduct.Text.Trim.ToUpper Then
                        Return row.Index
                    End If
                Else
                    Exit For
                End If
            Next

            If strSearchByCode.Contains("CODE:") Then
                For Each row As DataGridViewRow In fgProducts.Rows
                    If Not IsNothing(row.Cells.Item("Code").Value) Then
                        If row.Cells.Item("Code").Value.ToString.ToUpper.StartsWith(txtProduct.Text.Trim.ToUpper) Then
                            Return row.Index
                        End If
                    Else
                        Exit For
                    End If
                Next
                For Each row As DataGridViewRow In fgProducts.Rows
                    If Not IsNothing(row.Cells.Item("Name").Value) Then
                        If row.Cells.Item("Name").Value.ToString.ToUpper.StartsWith(txtProduct.Text.Trim.ToUpper) Then
                            Return row.Index
                        End If
                    Else
                        Exit For
                    End If
                Next
            Else
                For Each row As DataGridViewRow In fgProducts.Rows
                    row.Selected = False
                    If Not IsNothing(row.Cells.Item("Name").Value) Then
                        If row.Cells.Item("Name").Value.ToString.ToUpper = txtProduct.Text.Trim.ToUpper OrElse row.Cells.Item("Code").Value.ToString.ToUpper.StartsWith(txtProduct.Text.Trim.ToUpper) Then
                            Return row.Index
                        End If
                    Else
                        Exit For
                    End If
                Next
                For Each row As DataGridViewRow In fgProducts.Rows
                    If Not IsNothing(row.Cells.Item("Name").Value) Then
                        If row.Cells.Item("Name").Value.ToString.ToUpper.StartsWith(txtProduct.Text.Trim.ToUpper) Then
                            Return row.Index
                        End If
                    Else
                        Exit For
                    End If
                Next
            End If

            Return 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGrid")
            Return 0
        End Try
    End Function


End Class
